package com.lastproject.web.bean;

import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import com.lastproject.dto.CategoryDTO;
import com.lastproject.dto.SubcategoryDTO;
import com.lastproject.dto.SurveyDTO;
import com.lastproject.dto.TopicDTO;
import com.lastproject.dto.TopicWithNamesDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.CheckPermissionDAORemote;
import com.lastproject.ejb.dao.SurveyDAORemote;
import com.lastproject.ejb.dao.TopicDAORemote;

public class SurveyBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6775095649876038239L;

	@EJB
	SurveyDAORemote surveyDao;

	@EJB
	TopicDAORemote topicDAORemote;

	private List<TopicWithNamesDTO> topics;

	private TopicDTO tDTO;

	private SurveyDTO surveyDTO;

	private CategoryDTO categoryDTO;

	private SubcategoryDTO subcategoryDTO;

	private boolean dateNotValid = false;

	private boolean firstDateNotValid = false;

	public boolean hasPermission = true;
	
	public boolean isHasPermission() {
		return hasPermission;
	}

	public void setHasPermission(boolean hasPermission) {
		this.hasPermission = hasPermission;
	}

	public String getNoRights() {
		return noRights;
	}

	public void setNoRights(String noRights) {
		this.noRights = noRights;
	}

	private String noRights = "";

	public SurveyBean() {
	}

	@PostConstruct
	public void met() {
		this.topics = topicDAORemote.findAllWithNames();
	}
	

	@EJB
	CheckPermissionDAORemote checkPermissionDAORemote;
	
	public void preRenderView(ComponentSystemEvent event) {
        if (!checkPermissionDAORemote.hasSurveyManagementRights(true)){
        	FacesContext fc = FacesContext.getCurrentInstance();
        	ExternalContext ec = fc.getExternalContext();
        	try {
				ec.redirect("restrictedAccess.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }

	/**
	 * Create a Survey
	 * 
	 * @return Returns a String containing [nextPage].xhtml
	 */
	public String create() {
		noRights = "";
		hasPermission = true;
		FacesContext f = FacesContext.getCurrentInstance();
		UserDTO userDTO = (UserDTO) f.getExternalContext().getSessionMap()
				.get("userDTO");

		surveyDTO.setTitle(surveyDTO.getTitle());
		surveyDTO.setDescription(surveyDTO.getDescription());
		surveyDTO.setStartDate(new Date());
		surveyDTO.setEndDate(surveyDTO.getEndDate());

		surveyDTO.setIdUser(userDTO.getId());

		surveyDTO.setIdTopic(tDTO.getId());
		surveyDTO.setFinished(false);

		if (datesValidation(surveyDTO.getStartDate(), surveyDTO.getEndDate()) == false) {
			setDateNotValid(true);
			return "surveyCreate.xhtml";
		} else
			setDateNotValid(false);

		boolean ok = surveyDao.create(true, surveyDTO);
		if (!ok) {
			noRights = "You do not have the permission to create quizes!";
			hasPermission = false;
			return "surveyCreate.xhtml";
		}
		return "survey.xhtml";
	}

	/**
	 * Validates the two parameters. d1 must be before d2
	 * 
	 * @param d1
	 *            Date parameter containing firstDate
	 * @param d2
	 *            Date parameter containing endDate
	 * @return Returns a boolean containing if the two dates are valid
	 */
	public boolean datesValidation(Date d1, Date d2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		String start = sdf.format(d1);
		String end = sdf.format(d2);
		String[] startDate;
		startDate = start.split("-");
		String[] endDate;
		endDate = end.split("-");
		if (Integer.parseInt(startDate[0]) < Integer.parseInt(endDate[0])) {
			return true;
		} else {
			if (Integer.parseInt(startDate[1]) < Integer.parseInt(endDate[1])) {
				return true;
			} else {
				if (Integer.parseInt(startDate[2]) < Integer
						.parseInt(endDate[2])) {
					return true;
				}
			}
		}
		return false;
	}

	public TopicDTO gettDTO() {
		return tDTO;
	}

	public void settDTO(TopicDTO tDTO) { // SETT ???
		this.tDTO = tDTO;
	}

	public SurveyDTO getSurveyDTO() {
		return surveyDTO;
	}

	public void setSurveyDTO(SurveyDTO surveyDTO) {
		this.surveyDTO = surveyDTO;
	}

	public List<TopicWithNamesDTO> getTopics() {
		return topicDAORemote.findAllWithNames();
	}

	public void setTopics(List<TopicWithNamesDTO> topics) {
		this.topics = topics;
	}

	public CategoryDTO getCategoryDTO() {
		return categoryDTO;
	}

	public void setCategoryDTO(CategoryDTO categoryDTO) {
		this.categoryDTO = categoryDTO;
	}

	public SubcategoryDTO getSubcategoryDTO() {
		return subcategoryDTO;
	}

	public void setSubcategoryDTO(SubcategoryDTO subcategoryDTO) {
		this.subcategoryDTO = subcategoryDTO;
	}

	public boolean isDateNotValid() {
		return dateNotValid;
	}

	public void setDateNotValid(boolean dateNotValid) {
		this.dateNotValid = dateNotValid;
	}

	public boolean isFirstDateNotValid() {
		return firstDateNotValid;
	}

	public void setFirstDateNotValid(boolean firstDateNotValid) {
		this.firstDateNotValid = firstDateNotValid;
	}

}
